package com.rlevin.java.chmod;
import junit.framework.TestCase;
/**
 * Tests the Permissions/Ugo functionality
 * @author roblevin
 */
public class PermissionsTest extends TestCase {

	public PermissionsTest(String name) {
		super(name);
	}
	protected void setUp() throws Exception {
		super.setUp();
	}
	protected void tearDown() throws Exception {
		super.tearDown();
	}
	
	public void testSet() throws Exception {
		Permissions permissions = new Permissions();
		permissions.set(Permissions.Flag.READ, Permissions.Flag.WRITE);
		assertTrue(permissions.isOn(Permissions.Flag.READ));
		assertTrue(permissions.isOn(Permissions.Flag.WRITE));
		assertFalse(permissions.isOn(Permissions.Flag.EXECUTE));
		assertTrue(permissions.isOff(Permissions.Flag.EXECUTE));
	}
	
	/**
	 * Test the seven possible permission levels.
	 */
	public void testPermissionLevelOFF() throws Exception {
		Permissions permissions = new Permissions();	// settings defaults to 0x0 or OFF if you will ;)
		assertTrue(permissions.getCurrentPermissionLevelAsString().equalsIgnoreCase(
				Permissions.PermissionLevel.OFF.toString()));
	}
	
	public void testPermissionLevelEXECUTE() throws Exception {
		Permissions permissions = new Permissions(Permissions.Flag.EXECUTE);	
		assertTrue(permissions.getCurrentPermissionLevelAsString().equalsIgnoreCase(
				Permissions.PermissionLevel.EXECUTE.toString()));
	}
	
	public void testPermissionLevelWRITE() throws Exception {
		Permissions permissions = new Permissions(Permissions.Flag.WRITE);	
		assertTrue(permissions.getCurrentPermissionLevelAsString().equalsIgnoreCase(
				Permissions.PermissionLevel.WRITE.toString()));
	}
	
	public void testPermissionLevelWRITE_EXECUTE() throws Exception {
		Permissions permissions = new Permissions(
				Permissions.Flag.WRITE, Permissions.Flag.EXECUTE);
		assertTrue(permissions.getCurrentPermissionLevelAsString().equalsIgnoreCase(
				Permissions.PermissionLevel.WRITE_EXECUTE.toString()));
	}
	
	public void testPermissionLevelREAD() throws Exception {
		Permissions permissions = new Permissions();
		permissions.set(Permissions.Flag.READ);
		assertTrue(permissions.getCurrentPermissionLevelAsString().equalsIgnoreCase(
				Permissions.PermissionLevel.READ.toString()));
	}
	
	public void testPermissionLevelREAD_EXECUTE() throws Exception {
		Permissions permissions = new Permissions();
		permissions.set(Permissions.Flag.READ, Permissions.Flag.EXECUTE);		
		assertTrue(permissions.getCurrentPermissionLevelAsString().equalsIgnoreCase(
				Permissions.PermissionLevel.READ_EXECUTE.toString()));
	}
	
	public void testPermissionLevelREAD_WRITE() throws Exception {
		Permissions permissions = new Permissions();
		permissions.set(Permissions.Flag.READ, Permissions.Flag.WRITE);		
		assertTrue(permissions.getCurrentPermissionLevelAsString().equalsIgnoreCase(
				Permissions.PermissionLevel.READ_WRITE.toString()));
	}
	
	public void testPermissionLevelREAD_WRITE_EXECUTE() throws Exception {
		Permissions permissions = new Permissions();
		permissions.set(Permissions.Flag.READ, Permissions.Flag.WRITE, Permissions.Flag.EXECUTE);		
		assertTrue(permissions.getCurrentPermissionLevelAsString().equalsIgnoreCase(
				Permissions.PermissionLevel.READ_WRITE_EXECUTE.toString()));
	}
	
	// Throwaway test. Testing binary bit operators '&' '|' '^'
	public void testBinaryOperators() throws Exception {
		assertEquals(0, 0 & 0);
		assertEquals(0, 0 & 1);
		assertEquals(0, 1 & 0);
		assertEquals(1, 1 & 1);
		assertEquals(0, 0 | 0);
		assertEquals(1, 0|1);
		assertEquals(1, 1|0);
		assertEquals(1, 1|1);
		assertEquals(0, 0 ^ 0);
		assertEquals(0, 1^1);
		assertEquals(1, 1 ^ 0);
		assertEquals(1, 0 ^ 1);
	}
}
